import { PageDto } from "../models/page.model";
import { UaUserDeckEntity, UaUserDeckDto, UaUserDeckSearchDto } from "../models/ua-user-deck.model";
import { GlobalService } from "./global.service";
import { LocalStorage } from "./local-storage.service";


export class UserDeckService {
  static PATH = "/ua/user/deck";
  static searchWithPage(data: UaUserDeckSearchDto, page: number) {
    return new Promise<PageDto<UaUserDeckEntity>>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UserDeckService.PATH}/search?page=${page}`,
          method: "POST",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          data,
          success: (response) => resolve(response.data as PageDto<UaUserDeckEntity>),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static findBySn(Sn: string) {
    return new Promise<UaUserDeckDto>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UserDeckService.PATH}/sn/${Sn}`,
          method: "GET",
          success: (response) => resolve(response.data as UaUserDeckDto),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static getQrCode(sn: string) {
    return new Promise<string>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}/getQrCode/${sn}`,
          method: "GET",
          success: (response) => resolve(response.data as string),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static findById(id: string | number) {
    return new Promise<UaUserDeckDto>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UserDeckService.PATH}/${id}`,
          method: "GET",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          success: (response) => resolve(response.data as UaUserDeckDto),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static removeById(id: number) {
    return new Promise<UaUserDeckDto>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UserDeckService.PATH}/${id}`,
          method: "DELETE",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          success: (response) => resolve(response.data as UaUserDeckDto),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static createDeck(data: UaUserDeckDto) {
    return new Promise<UaUserDeckEntity>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UserDeckService.PATH}/`,
          method: "POST",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          data,
          success: (response) => resolve(response.data as UaUserDeckEntity),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static getTags() {
    return new Promise<string[]>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UserDeckService.PATH}/tags`,
          method: "GET",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          success: (response) => resolve(response.data as string[]),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static addTag(tag: string) {
    return new Promise<boolean>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UserDeckService.PATH}/tag/${tag}`,
          method: "GET",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          success: (response) => resolve(response.data as any),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static removeTag(tag: string) {
    return new Promise<boolean>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UserDeckService.PATH}/tag/${tag}`,
          method: "DELETE",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          success: (response) => resolve(response.data as any),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static updateDeck(data: UaUserDeckDto) {
    return new Promise<UaUserDeckEntity>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UserDeckService.PATH}/`,
          method: "PUT",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          data,
          success: (response) => resolve(response.data as UaUserDeckEntity),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static genPdf(deckId: string, needEffect: boolean) {
    return new Promise<{
      url: string
    }>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UserDeckService.PATH}/genPdfByDeckId`,
          method: "POST",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          data: {
            deckId,
            needEffect
          },
          success: (response) => resolve(response.data as {
            url: string
          }),
          fail: (err) => reject(err)
        });
      }
    )
  }
}
